<!--  -->
<template>
  <div class="student">
    <h3>学生名:{{name}}</h3>
    <h3>学生性别:{{gender}}</h3>
    <h2>从School.vue组件接收到的学校名是:{{schoolName}}</h2>
  </div>
</template>

<script>
import pubsub from "pubsub-js"
export default {
  data () {
    return {
      name: "张涛",
      gender: "male",
      schoolName: ''
    }
  },
  //生命周期 - 创建完成（访问当前this实例）
  created () {

  },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted () {
    // 2.生成id
    this.pubId = pubsub.subscribe("schoolName", (msgName, data) => {
      // 1.第一个参数是传过来的事件名，第二个参数才是传过来的数据
      console.log(msgName, data);
      this.schoolName = data
    })
  },
  beforeDestroy () {
    // 3.取消订阅
    pubsub.unsubscribe(this.pubId)
  }
}
</script>
<style scoped>
.student {
  width: 60%;
  height: 150px;
  background-color: lightblue;
  margin: 30px;
}
</style>